package com.css.fxfzypg.modules.baseData.repository.impl;

import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.baseData.entity.AnrrsmEntity;
import com.css.fxfzypg.modules.baseData.queryParams.AnrrsmQueryParams;
import com.css.fxfzypg.modules.baseData.repository.AnrrsmRepository;
import com.css.fxfzypg.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-08-29 15:27
 */
@Repository
public class AnrrsmRepositoryImpl implements AnrrsmRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public AnrrsmEntity saveAnrrsm(AnrrsmEntity anrrsmEntity) {
        String sql = "";
        sql = "insert into data_an_rrsm values(:arrsmId, :arrsnProvince, :arrsmCity, :arrsmPrefecture, :arrsmKhdoed, :arrsmBuilding, :arrsmLifeLine, :arrsmMajorInfrastructure" +
                ", :arrsmOarrs, :arrsmCreatetime, :arrsmUpdatetime, :arrsmIsdelete,:provinceId,:cityId,:prefectureId) ";

        SqlParameterSource source = new BeanPropertySqlParameterSource(anrrsmEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return anrrsmEntity;
    }

    @Override
    public Object getAnrrsmById(String id) {
        String sql="";
        sql="select * from data_an_rrsm where arrsm_id='"+id+"'";
        AnrrsmEntity anrrsmEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(AnrrsmEntity.class));
        return anrrsmEntity;
    }

    @Override
    public AnrrsmEntity updateAnrrsm(AnrrsmEntity anrrsmEntity) {
        String sql="";
        sql="update data_an_rrsm set arrsn_province = :arrsnProvince,arrsm_city = :arrsmCity,arrsm_prefecture = :arrsmPrefecture, arrsm_khdoed =:arrsmKhdoed,arrsm_building =:arrsmBuilding,arrsm_life_line=:arrsmLifeLine,arrsm_major_infrastructure=:arrsmMajorInfrastructure ,arrsm_oarrs=:arrsmOarrs, arrsm_updatetime=:arrsmUpdatetime, province_id=:provinceId, city_id=:cityId, prefecture_id =:prefectureId where arrsm_id=:arrsmId";
        SqlParameterSource source = new BeanPropertySqlParameterSource(anrrsmEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return anrrsmEntity;
    }

    @Override
    public void deleteAnrrsm(List<String> ids, Timestamp currentTimestamp) {
        String sql = "update data_an_rrsm set arrsm_isdelete = '" + YNEnum.N.toString() + "' ," +
                " arrsm_updatetime = '"+ currentTimestamp +"' " +
                " where arrsm_id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public Map<String,Object> getAllAnrrsmByParams(AnrrsmQueryParams queryParams) {
        StringBuilder sql = new StringBuilder(" select  u.* from data_an_rrsm u ");

        StringBuilder orderSql = new StringBuilder(" order by u.arrsm_createtime");

        StringBuilder whereSql = new StringBuilder(" where u.arrsm_isdelete = '" + YNEnum.Y.toString() + "'");

        if (!PlatformObjectUtils.isEmpty(queryParams.getProvinceId())) {
            whereSql.append(" and u.province_id ='"+queryParams.getProvinceId()+"'");
        }

        if (!PlatformObjectUtils.isEmpty(queryParams.getCityId())) {
            whereSql.append(" and u.city_id  ='" + queryParams.getCityId() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getPrefectureId())) {
                whereSql.append(" and u.prefecture_id ='" + queryParams.getPrefectureId() + "'");
        }

        int firstIndex=(queryParams.getCurPage()-1) * queryParams.getPageSize();
        int lastIndex=queryParams.getCurPage() * queryParams.getPageSize();
        sql.append(whereSql).append(orderSql).toString();
        StringBuilder limitSql = new StringBuilder(" limit "+lastIndex+" offset "+ firstIndex);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(AnrrsmEntity.class)).size();
        List<AnrrsmEntity> anrrsmEntities = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(AnrrsmEntity.class));
        Map<String,Object> map=new HashMap();
        map.put("total",total);
        map.put("rows",anrrsmEntities);
        return map;

    }

}
